//
//  SavedLocationForecastViewController.swift
//  SwiftWeather
//
//  Created by shen on 2023/8/28.
//

import UIKit

class SavedLocationForecastViewController: BaseViewController {
    private let weather: Weather?

    init(weather: Weather?) {
        self.weather = weather
        super.init(nibName: nil, bundle: nil)
    }

    @available(*, unavailable)
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        forecastView.viewModel = ForecastViewViewModel(weather: weather)
    }

    // MARK: - Views

    private lazy var forecastView = ForecastView()

    private lazy var closeButton: UIBarButtonItem = .init(
        title: "Close",
        style: .plain,
        target: self,
        action: #selector(closeButtonTapped)
    )

    // MARK: - Methods

    override func configureAppearance() {
        super.configureAppearance()
        setupNavigationBar()
    }

    override func setupViews() {
        view.addSubview(forecastView, useAutoLayout: true)
    }

    override func constraintViews() {
        NSLayoutConstraint.activate([
            forecastView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
            forecastView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor),
            forecastView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor),
            forecastView.bottomAnchor.constraint(equalTo: view.bottomAnchor)
        ])
    }
}

// MARK: - Actions

@objc private extension SavedLocationForecastViewController {
    func closeButtonTapped() {
        self.dismiss(animated: true)
    }
}

// MARK: - Private methods

private extension SavedLocationForecastViewController {
    func setupNavigationBar() {
        navigationItem.leftBarButtonItem = closeButton
        navigationController?.navigationBar.tintColor = .label
    }
}
